/*******************************************************************************
 * Copyright (c) 2012 Philip Douglas.
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/.
 ******************************************************************************/
package net.pdouglas.revisiondroid;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;

import net.pdouglas.revision.TimeFormatter;

public class Counter extends TextView {
    private int count = 0;
    private CounterState state = CounterState.POSITIVE;

    public Counter(Context context) {
        super(context);
        setCounter();
    }

    public Counter(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCounter();
    }

    public Counter(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCounter();
    }
    
    public int increment() {
        count++;
        setCounter();
        return count;
    }

    public int decrement() {
        count--;
        setCounter();
        return count;
    }
    
    public void setTime(int time) {
        // TODO Input validation
        count = time;
        setCounter();
    }

    private void setCounter() {
        TimeFormatter f = new TimeFormatter(count);
        setText(f.toString());
        CounterState gen = CounterState.update(state, count);
        if (gen == CounterState.UNCHANGED) {
        	return;
        } else {
        	state = gen;
        }

    	Resources res = getResources();
        switch (state) {
        	case BELOW10:
            	setTextSize(TypedValue.COMPLEX_UNIT_PX, 
            			res.getDimension(R.dimen.counter_text_size_neg_long));
            	break;
        	case NEGATIVE:
        		setTextSize(TypedValue.COMPLEX_UNIT_PX, 
        				res.getDimension(R.dimen.counter_text_size_neg));
        		break;
        	case POSITIVE:
        		setTextSize(TypedValue.COMPLEX_UNIT_PX, 
        				res.getDimension(R.dimen.counter_text_size));
        		break;
        	case ABOVE10:
        		setTextSize(TypedValue.COMPLEX_UNIT_PX, 
        				res.getDimension(R.dimen.counter_text_size_long));
        		break;
        }
    }
}
